package org.icress.fusion.snapshot

class CreditControlController {

    def index = { redirect(action: "list", params: params) }

    def creditControlService

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 100,  100)
        render view : '/snapshot/creditControl/list', model : [creditControlInstanceList: creditControlService.list(params), creditControlInstanceTotal: creditControlService.count()]
    }

    def create = {
        def creditControlInstance = new CreditControl()
        creditControlInstance.properties = params
        render view : '/snapshot/creditControl/create', model : [creditControlInstance: creditControlInstance]
    }

    def save = {
        def creditControlInstance = new CreditControl(params)
        if (!creditControlInstance.hasErrors() && creditControlInstance.save()) {
            flash.message = "creditControl.created"
            flash.args = [creditControlInstance.id]
            flash.defaultMessage = "CreditControl ${creditControlInstance.id} created"
            redirect(action: "show", id: creditControlInstance.id)
        }
        else {
            render(view: "/snapshot/creditControl/create", model: [creditControlInstance: creditControlInstance])
        }
    }

    def show = {
        def creditControlInstance = creditControlService.show(params.id)
        if (!creditControlInstance) {
            flash.message = "creditControl.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "CreditControl not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/snapshot/creditControl/show', model : [creditControlInstance: creditControlInstance]
        }
    }

    def edit = {
        def creditControlInstance = creditControlService.edit(params.id)
        if (!creditControlInstance) {
            flash.message = "creditControl.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "CreditControl not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/snapshot/creditControl/edit', model : [creditControlInstance: creditControlInstance]
        }
    }

    def update = {
        def creditControlInstance = creditControlService.edit(params.id)
        if (creditControlInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (creditControlInstance.version > version) {
                    
                    creditControlInstance.errors.rejectValue("version", "snapshot.creditControl.optimistic.locking.failure", "Another user has updated this CreditControl while you were editing")
                    render(view: "/snapshot/creditControl/edit", model: [creditControlInstance: creditControlInstance])
                    return
                }
            }
            creditControlInstance.properties = params
            if (!creditControlInstance.hasErrors() && creditControlInstance.save()) {
                flash.message = "snapshot.creditControl.updated"
                flash.args = [params.id]
                flash.defaultMessage = "CreditControl ${params.id} updated"
                redirect(action: "show", id: creditControlInstance.id)
            }
            else {
                render(view: "/snapshot/creditControl/edit", model: [creditControlInstance: creditControlInstance])
            }
        }
        else {
            flash.message = "snapshot.creditControl.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "CreditControl not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def creditControlInstance = creditControlService.delete(params.id)
        if (creditControlInstance) {
            try {
                creditControlInstance.delete()
                flash.message = "snapshot.creditControl.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "CreditControl ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "snapshot.creditControl.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "CreditControl ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "snapshot.creditControl.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "CreditControl not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
